home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / unix / mp14tar.z / mp14tar / mpack / unixpk.c < prev    next >
C/C++ Source or Header  |  1994-06-01  |  7KB  |  299 lines

  1. /* (C) Copyright 1993 by John G. Myers
  2.  * All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software
  5.  * and its documentation for any purpose is hereby granted without
  6.  * fee, provided that the above copyright notice appear in all copies
  7.  * and that both that copyright notice and this permission notice
  8.  * appear in supporting documentation, and that the name of John G.
  9.  * Myers not be used in advertising or publicity pertaining to
  10.  * distribution of the software without specific, written prior
  11.  * permission.  John G. Myers makes no representations about the
  12.  * suitability of this software for any purpose.  It is provided "as
  13.  * is" without express or implied warranty.
  14.  *
  15.  * JOHN G. MYERS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17.  * FITNESS, IN NO EVENT SHALL JOHN G. MYERS BE LIABLE FOR ANY SPECIAL,
  18.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  19.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  21.  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  */
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include "common.h"
  27. #include "version.h"
  28. #include "xmalloc.h"
  29.  
  30. #define MAXADDRESS 100
  31.  
  32. extern char *getenv();
  33.  
  34. extern int errno;
  35. extern int optind;
  36. extern char *optarg;
  37.  
  38. main(argc, argv)
  39. int argc;
  40. char **argv;
  41. {
  42.     int opt;
  43.     char *fname = 0;
  44.     char *subject = 0;
  45.     char *descfname = 0;
  46.     long maxsize = 0;
  47.     char *outfname = 0;
  48.     char *newsgroups = 0;
  49.     char *ctype = 0;
  50.     char *headers = 0;
  51.     int i;
  52.     char *p;
  53.     char sbuf[1024];
  54.     char fnamebuf[4096];
  55.     int part;
  56.     FILE *infile;
  57.     FILE *descfile = 0;
  58.  
  59.     if ((p = getenv("SPLITSIZE")) && *p >= '0' && *p <= '9') {
  60.     maxsize = atoi(p);
  61.     }
  62.  
  63.     while ((opt = getopt(argc, argv, "s:d:m:c:o:n:")) != EOF) {
  64.     switch (opt) {
  65.     case 's':
  66.         subject = optarg;
  67.         break;
  68.  
  69.     case 'd':
  70.         descfname = optarg;
  71.         break;
  72.  
  73.     case 'm':
  74.         maxsize = atoi(optarg);
  75.         break;
  76.  
  77.     case 'c':
  78.         ctype = optarg;
  79.         break;
  80.  
  81.     case 'o':
  82.         outfname = optarg;
  83.         break;
  84.  
  85.     case 'n':
  86.         newsgroups = optarg;
  87.         break;
  88.  
  89.     default:
  90.         usage();
  91.  
  92.     }
  93.     }
  94.  
  95.     if (ctype) {
  96.     if (!cistrncmp(ctype, "text/", 5)) {
  97.         fprintf(stderr, "This program is not appropriate for encoding textual data\n");
  98.         exit(1);
  99.     }
  100.     if (cistrncmp(ctype, "application/", 12) && cistrncmp(ctype, "audio/", 6) &&
  101.         cistrncmp(ctype, "image/", 6) && cistrncmp(ctype, "video/", 6)) {
  102.         fprintf(stderr, "Content type must be subtype of application, audio, image, or video\n");
  103.         exit(1);
  104.     }
  105.     }
  106.  
  107.     if (optind == argc) {
  108.     fprintf(stderr, "An input file must be specified\n");
  109.     usage();
  110.     }
  111.     fname = argv[optind++];
  112.  
  113.     /* Must have exactly one of -o, -n, or destination addrs */
  114.     if (optind == argc) {
  115.     if (outfname && newsgroups) {
  116.         fprintf(stderr, "The -o and -n switches are mutually exclusive.\n");
  117.         usage();
  118.     }
  119.     if (!outfname && !newsgroups) {
  120.         fprintf(stderr, "Either an address or one of the -o or -n switches is required\n");
  121.         usage();
  122.     }
  123.     if (newsgroups) {
  124.         headers = xmalloc(strlen(newsgroups) + 25);
  125.         sprintf(headers, "Newsgroups: %s\n", newsgroups);
  126.     }
  127.     }
  128.     else {
  129.     if (outfname) {
  130.         fprintf(stderr, "The -o switch and addresses are mutually exclusive.\n");
  131.         usage();
  132.     }
  133.     if (newsgroups) {
  134.         fprintf(stderr, "The -n switch and addresses are mutually exclusive.\n");
  135.         usage();
  136.     }
  137.     headers = xmalloc(strlen(argv[optind]) + 25);
  138.     sprintf(headers, "To: %s", argv[optind]);
  139.     for (i = optind+1; i < argc; i++) {
  140.         headers = xrealloc(headers, strlen(headers)+strlen(argv[i]) + 25);
  141.         strcat(headers, ",\n\t");
  142.         strcat(headers, argv[i]);
  143.     }
  144.     strcat(headers, "\n");
  145.     }
  146.  
  147.     if (!subject) {
  148.     fputs("Subject: ", stdout);
  149.     fflush(stdout);
  150.     if (!fgets(sbuf, sizeof(sbuf), stdin)) {
  151.         fprintf(stderr, "A subject is required\n");
  152.         usage();
  153.     }
  154.     if (p = strchr(sbuf, '\n')) *p = '\0';
  155.     subject = sbuf;
  156.     }    
  157.  
  158.     if (!outfname) {
  159.     if (getenv("TMPDIR")) {
  160.         strcpy(fnamebuf, getenv("TMPDIR"));
  161.     }
  162.     else {
  163.         strcpy(fnamebuf, "/tmp");
  164.     }
  165.     strcat(fnamebuf, "/mpackXXXXXX");
  166.     mktemp(fnamebuf);
  167.     outfname = strsave(fnamebuf);
  168.     }
  169.  
  170.     infile = fopen(fname, "r");
  171.     if (!infile) {
  172.     os_perror(fname);
  173.     exit(1);
  174.     }
  175.  
  176.     if (descfname) {
  177.     descfile = fopen(descfname, "r");
  178.     if (!descfile) {
  179.         os_perror(descfname);
  180.         exit(1);
  181.     }
  182.     }
  183.  
  184.     if (encode(infile, (FILE *)0, fname, descfile, subject, headers,
  185.            maxsize, ctype, outfname)) exit(1);
  186.  
  187.     if (optind < argc || newsgroups) {
  188.     for (part = 0;;part++) {
  189.         sprintf(fnamebuf, "%s.%02d", outfname, part);
  190.         infile = fopen(part ? fnamebuf : outfname, "r");
  191.         if (!infile) {
  192.         if (part) break;
  193.         continue;
  194.         }
  195.         if (newsgroups) {
  196.         inews(infile, newsgroups);
  197.         }
  198.         else {
  199.         sendmail(infile, argv, optind);
  200.         }
  201.         fclose(infile);
  202.         remove(part ? fnamebuf : outfname);
  203.     }
  204.     }
  205.  
  206.     exit(0);
  207. }
  208.  
  209. usage()
  210. {
  211.     fprintf(stderr, "mpack version %s\n", MPACK_VERSION);
  212.     fprintf(stderr, 
  213. "usage: mpack [-s subj] [-d file] [-m maxsize] [-c content-type] file address...\n");
  214.     fprintf(stderr, 
  215. "       mpack [-s subj] [-d file] [-m maxsize] [-c content-type] -o file file\n");
  216.     fprintf(stderr, 
  217. "       mpack [-s subj] [-d file] [-m maxsize] [-c content-type] -n groups file\n");
  218.     exit(1);
  219. }
  220.  
  221. sendmail(infile, addr, start)
  222. FILE *infile;
  223. char **addr;
  224. int start;
  225. {
  226.     int status;
  227.     int pid;
  228.  
  229.     if (start < 2) abort();
  230.  
  231. #ifdef SCO
  232.     addr[--start] = "execmail";
  233. #else
  234.     addr[--start] = "-oi";
  235.     addr[--start] = "sendmail";
  236. #endif
  237.  
  238.     do {
  239.     pid = fork();
  240.     } while (pid == -1 && errno == EAGAIN);
  241.     
  242.     if (pid == -1) {
  243.     perror("fork");
  244.     return;
  245.     }
  246.     if (pid != 0) {
  247.     while (pid != wait(&status));
  248.     return;
  249.     }
  250.  
  251.     dup2(fileno(infile), 0);
  252.     fclose(infile);
  253. #ifdef SCO
  254.     execv("/usr/lib/mail/execmail", addr+start);
  255. #else
  256.     execv("/usr/lib/sendmail", addr+start);
  257.     execv("/usr/sbin/sendmail", addr+start);
  258. #endif
  259.     perror("execv");
  260.     _exit(1);
  261. }
  262.  
  263. inews(infile)
  264. FILE *infile;
  265. {
  266.     int status;
  267.     int pid;
  268.  
  269.     do {
  270.     pid = fork();
  271.     } while (pid == -1 && errno == EAGAIN);
  272.     
  273.     if (pid == -1) {
  274.     perror("fork");
  275.     return;
  276.     }
  277.     if (pid != 0) {
  278.     while (pid != wait(&status));
  279.     return;
  280.     }
  281.  
  282.     dup2(fileno(infile), 0);
  283.     fclose(infile);
  284.     execlp("inews", "inews", "-h", "-S", (char *)0);
  285.     execl("/usr/local/news/inews", "inews", "-h", "-S", (char *)0);
  286.     execl("/usr/local/lib/news/inews", "inews", "-h", "-S", (char *)0);
  287.     execl("/etc/inews", "inews", "-h", "-S", (char *)0);
  288.     execl("/usr/etc/inews", "inews", "-h", "-S", (char *)0);
  289.     execl("/usr/news/inews", "inews", "-h", "-S", (char *)0);
  290.     execl("/usr/news/bin/inews", "inews", "-h", "-S", (char *)0);
  291.     perror("execl");
  292.     _exit(1);
  293. }
  294.  
  295. warn()
  296. {
  297.     abort();
  298. }
  299.